package com.choudou5.solr.job;

import com.choudou5.solr.bean.event.SolrEventConsumeBean;
import com.choudou5.solr.bean.event.SolrEventPublishBean;
import com.choudou5.solr.framework.constants.SolrConsts.EventType;
import com.choudou5.solr.framework.holder.SysContextHolder;
import com.choudou5.solr.service.ServiceHelper;
import com.choudou5.solr.service.event.SolrEventConsumeService;
import com.choudou5.solr.util.CollUtil;
import com.choudou5.solr.util.ConfigUtil;
import com.choudou5.solr.util.jdbc.DBUtil;
import com.choudou5.solr.util.jdbc.SqlFormatUtil;
import com.choudou5.solr.util.thread.TimerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wltea.analyzer.dic.Dictionary;

import java.util.Date;
import java.util.List;
import java.util.TimerTask;

/**
 * @name 事件任务
 * @author choudou5
 * @date 2018-07-26
 */
public class SolrEventJob {

    private static final Logger logger = LoggerFactory.getLogger(SolrEventJob.class);

    private static final String TASK_NAME = "solr-event-job";

    private static SolrEventConsumeService consumeService = ServiceHelper.singleton(SolrEventConsumeService.class);

    public static void watcher(){
        if(ConfigUtil.getSysMode().equals("standalone")){
            logger.warn("no open event sync");
            return;
        }
        TimerUtil.newTimer(TASK_NAME, new TimerTask() {
            @Override
            public void run() {
                logger.info("SolrEventJob start run");
                String currMacService = SysContextHolder.getServiceHost();
                String sql = SqlFormatUtil.format("SELECT a.* FROM `solr_event_publish` a LEFT JOIN `solr_event_consume` b ON a.`event_key` = b.`event_key` WHERE a.`producer` != ? AND a.status = 0 AND b.`consumer` != ? OR b.`consumer` IS NULL", currMacService, currMacService);
                try {
                    List<SolrEventPublishBean> eventList = DBUtil.findList(sql, SolrEventPublishBean.class);
                    if(CollUtil.isNotEmpty(eventList)){
                        for (SolrEventPublishBean eventBean : eventList) {
                            //消费事件
                            consumeEvent(eventBean, currMacService);
                        }
                    }
                } catch (Exception e) {
                    logger.error("SolrEventJob run excetion", e);
                }
            }
        }, 10*1000, 20*1000);
    }


    /**
     * 消费事件
     * @param eventBean
     * @param currMacService
     */
    private static void consumeEvent(SolrEventPublishBean eventBean, String currMacService){
        String eventKey = eventBean.getEventKey();
        EventType eventType = EventType.get(eventKey);
        if(eventType == null){
            logger.error(eventKey+" 事件不支持");
            return;
        }
        switch (eventType){
            case RELOAD_EXT_DIC:
                Dictionary.getSingleton().reLoadExtDict();
                break;
        }
        SolrEventConsumeBean consumeBean = new SolrEventConsumeBean();
        consumeBean.setEventKey(eventKey);
        consumeBean.setConsumer(currMacService);
        consumeBean.setCreateTime(new Date());
        consumeService.add(consumeBean);
    }

    public static void stop(){
        TimerUtil.cancel(TASK_NAME);
    }

}
